// The URL on your server where CesiumJS's static files are hosted.
window.CESIUM_BASE_URL = '/cesium'

import * as Cesium from 'cesium'
import '*/cesium/Build/Cesium/Widgets/widgets.css'

// Your access token can be found at: https://cesium.com/ion/tokens.
Cesium.Ion.defaultAccessToken =
  'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmMWQ1MmRkZS0zN2NkLTRlMGItOTBhZi1iZmY2YjNhMzU5NzgiLCJpZCI6NTYwMjgsImlhdCI6MTYyMTIzMDYxN30.dQ-lC_sJzVOTjOgWDlifr3-EcESBL-VKXWNjj2ksYwY'
// // Initialize the Cesium Viewer in the HTML element with the "cesiumContainer" ID.
// const viewer = new Cesium.Viewer('cesiumContainer', {
//   terrainProvider: Cesium.createWorldTerrain(),
// })
// // Add Cesium OSM Buildings, a global 3D buildings layer.
// const buildingTileset = viewer.scene.primitives.add(Cesium.createOsmBuildings())
// // Fly the camera to San Francisco at the given longitude, latitude, and height.
// viewer.camera.flyTo({
//   destination: Cesium.Cartesian3.fromDegrees(-122.4175, 37.655, 400),
//   orientation: {
//     heading: Cesium.Math.toRadians(0.0),
//     pitch: Cesium.Math.toRadians(-15.0),
//   },
// })

// 初始化（3D笛卡尔坐标）
export function init(id, type = 'img') {
  const config = {
    creditContainer: id,
    geocoder: false,
    homeButton: false,
    sceneModePicker: false,
    baseLayerPicker: false,
    navigationHelpButton: false,
    navigationInstructionsInitiallyVisible: false,
    animation: false,
    timeline: false,
    fullscreenButton: false,
    vrButton: false,
    infoBox: false,
    // terrainProvider: Cesium.createWorldTerrain({
    //   url: 'http://t0.tianditu.gov.cn/ter_c/wmts?tk=c837881faa32b209fad23703d36beb0a',
    //   requestVertexNormals: true,
    // }),
  }
  switch (type) {
    case 'vec':
      // 矢量底图
      config.imageryProvider = new Cesium.WebMapTileServiceImageryProvider({
        url: 'http://t0.tianditu.gov.cn/vec_w/wmts?tk=c837881faa32b209fad23703d36beb0a',
        layer: 'vec',
        style: 'default',
        tileMatrixSetID: 'w',
        format: 'tiles',
      })
      break
    case 'img':
      // 影像底图
      config.imageryProvider = new Cesium.WebMapTileServiceImageryProvider({
        url: 'http://t0.tianditu.gov.cn/img_w/wmts?tk=c837881faa32b209fad23703d36beb0a',
        layer: 'img',
        style: 'default',
        tileMatrixSetID: 'w',
        format: 'tiles',
      })
      break
    case 'ter':
      // 地形底图
      config.imageryProvider = new Cesium.WebMapTileServiceImageryProvider({
        url: 'http://t0.tianditu.gov.cn/ter_c/wmts?tk=c837881faa32b209fad23703d36beb0a',
        layer: 'ter',
        style: 'default',
        tileMatrixSetID: 'c',
        format: 'tiles',
      })
      break
  }
  const viewer = new Cesium.Viewer(id, config)
  switch (type) {
    case 'vec':
      // 矢量注记
      viewer.imageryLayers.addImageryProvider(
        new Cesium.WebMapTileServiceImageryProvider({
          url: 'http://t0.tianditu.gov.cn/cva_w/wmts?tk=c837881faa32b209fad23703d36beb0a',
          layer: 'cva',
          style: 'default',
          tileMatrixSetID: 'w',
          format: 'tiles',
        })
      )
      break
    case 'img':
      // 影像注记
      viewer.imageryLayers.addImageryProvider(
        new Cesium.WebMapTileServiceImageryProvider({
          url: 'http://t0.tianditu.gov.cn/cia_w/wmts?tk=c837881faa32b209fad23703d36beb0a',
          layer: 'cia',
          style: 'default',
          tileMatrixSetID: 'w',
          format: 'tiles',
        })
      )
      break
    case 'ter':
      // 地形注记
      viewer.imageryLayers.addImageryProvider(
        new Cesium.WebMapTileServiceImageryProvider({
          url: 'http://t0.tianditu.gov.cn/ter_w/wmts?tk=c837881faa32b209fad23703d36beb0a',
          layer: 'ter',
          style: 'default',
          tileMatrixSetID: 'w',
          format: 'tiles',
        })
      )
      break
  }
  return viewer
}

// 跳转功能
export function flyTo(viewer, option) {
  const ellipsoid = viewer?.scene?.globe?.ellipsoid // 弧度
  const {lng, lat, alt, heading = 0, pitch = -90, roll = 0, speed = 1} = option
  const options = {
    // 目的地对应笛卡尔坐标(经度, 维度, 海拔)
    destination: Cesium.Cartesian3.fromDegrees(lng, lat, alt, ellipsoid), // 使用弧度+经纬计算出笛卡尔坐标
    duration: speed, // 设置飞行持续时间，默认会根据距离来计算
    orientation: {
      heading: Cesium.Math.toRadians(heading), // 偏航角
      pitch: Cesium.Math.toRadians(pitch), // 仰俯角(+仰, -俯)
      roll: roll, // roll
    },
  }
  return viewer.camera.flyTo(options)
}

export default Cesium
